/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package apresentacao;

import java.awt.CardLayout;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Pedro
 * @author Thiago
 */
public class Painel extends javax.swing.JPanel {

    /* Atributo para que Painel possa usar os servicos de Controle */
    private Controle ctrl;
    
    /**
     * Creates new form Painel
     */
    public Painel(Controle ctrl) {
        initComponents();
        this.ctrl = ctrl;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MainPainel = new javax.swing.JPanel();
        PainelCentral = new javax.swing.JPanel();
        Login = new javax.swing.JPanel();
        userBox = new javax.swing.JTextField();
        passwdBox = new javax.swing.JPasswordField();
        userText = new javax.swing.JLabel();
        passwdText = new javax.swing.JLabel();
        SelectPrefixo = new javax.swing.JPanel();
        prefixoBox = new javax.swing.JComboBox();
        selectPref = new javax.swing.JLabel();
        buttonOK = new javax.swing.JButton();
        Result = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listResult = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        buttonBack = new javax.swing.JButton();
        label2 = new java.awt.Label();
        buttonSite = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(885, 519));

        MainPainel.setBackground(new java.awt.Color(0, 102, 204));
        MainPainel.setPreferredSize(new java.awt.Dimension(885, 519));

        PainelCentral.setBackground(new java.awt.Color(255, 255, 255));
        PainelCentral.setPreferredSize(new java.awt.Dimension(680, 427));
        PainelCentral.setLayout(new java.awt.CardLayout());

        Login.setPreferredSize(new java.awt.Dimension(820, 380));

        userBox.setText("admin");

        passwdBox.setText("jPasswordField1");

        userText.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        userText.setText("Usuário:");

        passwdText.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        passwdText.setText("Senha:");

        javax.swing.GroupLayout LoginLayout = new javax.swing.GroupLayout(Login);
        Login.setLayout(LoginLayout);
        LoginLayout.setHorizontalGroup(
            LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LoginLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userText)
                    .addComponent(passwdText))
                .addGap(32, 32, 32)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(userBox)
                    .addComponent(passwdBox, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)))
        );
        LoginLayout.setVerticalGroup(
            LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LoginLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userText, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwdText, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwdBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(274, Short.MAX_VALUE))
        );

        PainelCentral.add(Login, "cardLogin");

        prefixoBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        prefixoBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        selectPref.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        selectPref.setText("Selecione o prefixo das eletivas que você deseja:");

        buttonOK.setText("OK");
        buttonOK.setFocusable(false);
        buttonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout SelectPrefixoLayout = new javax.swing.GroupLayout(SelectPrefixo);
        SelectPrefixo.setLayout(SelectPrefixoLayout);
        SelectPrefixoLayout.setHorizontalGroup(
            SelectPrefixoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SelectPrefixoLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(SelectPrefixoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(SelectPrefixoLayout.createSequentialGroup()
                        .addComponent(prefixoBox, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(buttonOK))
                    .addComponent(selectPref, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(431, Short.MAX_VALUE))
        );
        SelectPrefixoLayout.setVerticalGroup(
            SelectPrefixoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SelectPrefixoLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(selectPref)
                .addGap(35, 35, 35)
                .addGroup(SelectPrefixoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prefixoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonOK))
                .addContainerGap(255, Short.MAX_VALUE))
        );

        PainelCentral.add(SelectPrefixo, "cardPrefixo");

        listResult.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Disciplina 1", "Disciplina 2", "Disciplina 3", "Disciplina 4", "Disciplina 5", "Disciplina 6", "Disciplina 7", "Disciplina 8", "Disciplina 9", "Disciplina 10", "Disciplina 11", "Disciplina 12", "Disciplina" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listResult);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel2.setText("Disciplinas que você pode cursar:");

        buttonBack.setText("Voltar");
        buttonBack.setFocusable(false);
        buttonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ResultLayout = new javax.swing.GroupLayout(Result);
        Result.setLayout(ResultLayout);
        ResultLayout.setHorizontalGroup(
            ResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ResultLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(ResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(418, Short.MAX_VALUE))
        );
        ResultLayout.setVerticalGroup(
            ResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ResultLayout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jLabel2)
                .addGap(35, 35, 35)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51)
                .addComponent(buttonBack)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        PainelCentral.add(Result, "cardResult");

        label2.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        label2.setForeground(new java.awt.Color(255, 255, 255));
        label2.setText("Universidade Estadual de Campinas");

        buttonSite.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        buttonSite.setText("DAC");
        buttonSite.setFocusable(false);
        buttonSite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSiteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout MainPainelLayout = new javax.swing.GroupLayout(MainPainel);
        MainPainel.setLayout(MainPainelLayout);
        MainPainelLayout.setHorizontalGroup(
            MainPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPainelLayout.createSequentialGroup()
                .addGroup(MainPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonSite, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(MainPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(MainPainelLayout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(PainelCentral, javax.swing.GroupLayout.PREFERRED_SIZE, 802, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(MainPainelLayout.createSequentialGroup()
                            .addGap(224, 224, 224)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        MainPainelLayout.setVerticalGroup(
            MainPainelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPainelLayout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(buttonSite)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PainelCentral, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(MainPainel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(MainPainel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed
        
        // pega o prefixo selecionado e seleciona as eletivas com esse prefixo
        String prefixo = prefixoBox.getSelectedItem().toString();
        String[] possiveisElet = ctrl.selecionaEletivas(prefixo);
        
        // modifica a lista para mostrar o resultado e muda para a tela de resultado
        listResult.removeAll();
        listResult.setListData(possiveisElet);
        
        CardLayout x = (CardLayout) PainelCentral.getLayout();
        x.show(PainelCentral, "cardResult");
    }//GEN-LAST:event_buttonOKActionPerformed

    private void buttonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBackActionPerformed
        /*
        Deixa o indice 0 do ComboBox selecionado e volta pra tela inicial de selecao de prefixos
        */
        prefixoBox.setSelectedIndex(0);
        CardLayout x = (CardLayout) PainelCentral.getLayout();
        x.show(PainelCentral, "cardPrefixo");
    }//GEN-LAST:event_buttonBackActionPerformed

    private void buttonSiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSiteActionPerformed
        // TODO add your handling code here:
        try {
            Runtime.getRuntime().exec("cmd.exe /C start " + "http://www.dac.unicamp.br/");
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }//GEN-LAST:event_buttonSiteActionPerformed
    
    public void iniciaPainel () {
        // Preenche o ComboBox com os prefixos das eletivas
        this.updateComboBox();
        
        // mostra a tela inicial de selecao de prefixo
        CardLayout x = (CardLayout) PainelCentral.getLayout();
        x.show(PainelCentral, "cardPrefixo");
    }
    
    public void updateComboBox () {
        // Remove tudo do ComboBox e recebe a lista de prefixos atraves do Controle
        prefixoBox.removeAllItems();
        ArrayList<String> prefixos = ctrl.getPrefixos();
        
        // Se a lista estiver vazia, trata do caso em que nao ha possiveis eletivas
        if (prefixos.isEmpty()) {
            this.semEletivas();
        }
        // senao, adiciona os prefixos no ComboBox
        else {
            for (String pref : prefixos) {
                prefixoBox.addItem(pref);
            }
        }
    }
    
    public void semEletivas() {
        prefixoBox.setVisible(false);
        buttonOK.setVisible(false);
        selectPref.setText("Não há eletivas possíveis de serem cursadas.");
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Login;
    private javax.swing.JPanel MainPainel;
    private javax.swing.JPanel PainelCentral;
    private javax.swing.JPanel Result;
    private javax.swing.JPanel SelectPrefixo;
    private javax.swing.JButton buttonBack;
    private javax.swing.JButton buttonOK;
    private javax.swing.JButton buttonSite;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label label2;
    private javax.swing.JList listResult;
    private javax.swing.JPasswordField passwdBox;
    private javax.swing.JLabel passwdText;
    private javax.swing.JComboBox prefixoBox;
    private javax.swing.JLabel selectPref;
    private javax.swing.JTextField userBox;
    private javax.swing.JLabel userText;
    // End of variables declaration//GEN-END:variables
}
